﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyShpMerger;

namespace ShpMerger
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            InitializeBackgoundWorker();
        }
        int Progress = 0;
        string[] files;
        string mergeFile;
        string sourceDerectiony;
        delegate void Updatecontrl();
        private void InitializeBackgoundWorker()
        {
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            try
            {
                ShapeFileMerger shapeFileMerger = new ShapeFileMerger();
                shapeFileMerger.Merge(files, mergeFile, worker, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Merge error,error code:" + ex.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        //If hide the progressbar use the code
        //private void Updateprogress()
        //{
        //    this.progressBar1.Visible = false;
        //}

        private void UpdatepLabel()
        {
            this.lblPercent.Text = "";
            this.lblPercent.Visible = false;
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            btnMerge.Enabled = false;
            btnCancel.Enabled = true;
            this.progressBar1.Value = e.ProgressPercentage;
            Progress = e.ProgressPercentage;
            this.lblPercent.Text = Progress.ToString() + "%";
            //    toolStripStatusLabel1.Text = "Complete" + Progress.ToString() + "%";
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                MessageBox.Show("Cancel merge，complte" + Progress.ToString() + "%.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {

                MessageBox.Show("Merge success.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //MessageBox.Show("Merge success.");
                //If hide the progressbar use the code
                //this.progressBar1.Invoke(new Updatecontrl(Updateprogress));

                this.lblPercent.Invoke(new Updatecontrl(UpdatepLabel));
            }
            this.btnMerge.Enabled = true;
            this.btnCancel.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Owner.Show();
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog myOpen = new OpenFileDialog();
            myOpen.Multiselect = true;
            myOpen.Filter = "Shape Files|*.shp";
            if (myOpen.ShowDialog() == DialogResult.OK)
            {
                //foreach (string fileName in myOpen.FileNames)
                //{
                listBox1.Items.AddRange(myOpen.FileNames);
                //}
            }
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            files = new string[listBox1.Items.Count];
            for (int n = 0; n < files.Length; n++)
            {
                files[n] = listBox1.Items[n].ToString();
            }
            mergeFile = textBox1.Text;
            Progress = 0;
            backgroundWorker1.RunWorkerAsync();
            this.lblPercent.Text = "";
            //  this.toolStripStatusLabel1.Text = "";
            this.lblPercent.Visible = true;
            this.progressBar1.Visible = true;
            btnCancel.Enabled = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            SaveFileDialog mySave = new SaveFileDialog();
            mySave.Filter = "Shape Files|*.shp";
            if (mySave.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = mySave.FileName;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.backgroundWorker1.CancelAsync();
            btnCancel.Enabled = false;
            btnMerge.Enabled = true;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            listBox1.Items.Remove(listBox1.SelectedItem);
        }

        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Owner.Show();
        }
    }
}
